from django.db import models
import json
import pytz
import datetime

# Create your models here.

class Student(models.Model):
    name = models.CharField(max_length=200) # 默认参数 blank=false null=false 
    num = models.CharField(max_length=200, unique=True)
    # username = models.CharField(max_length=200, unique=True)
    password = models.CharField(max_length=200)
    face_id = models.TextField(blank=True)
    classes = models.TextField(blank=True) # 所在班级的id列表

    def toDict(self):
        return {
            'id': str(self.id),
            'name': str(self.name),
            'num': str(self.num),
            'password': str(self.password),
            'face_id': str(self.face_id),
            'classes': str(self.classes),
        }
    
    def initFromDict(self, dic):
        self.name = dic.get('name', '')
        self.num = dic.get('num', '')
        self.password = dic.get('password', '')
        self.face_id = dic.get('face_id', '')
        self.classes = dic.get('classes', '')
        self.id = dic.get('id', None)
    
    def toJson(self):
        return json.dumps(self, default=self.toDict())

class Teacher(models.Model):
    username = models.CharField(max_length=200, unique=True)
    password = models.CharField(max_length=200)
    
    def toDict(self):
        return {
            'id': str(self.id),
            'username': str(self.username),
            'password': str(self.password),
            'teaches': str(self.teaches),
        }
    
    def initFromDict(self, dic):
        self.username = dic.get('username', '')
        self.password = dic.get('password', '')
        self.teaches = dic.get('teaches', '')
        self.id = dic.get('id', None)

class Class(models.Model):
    classname = models.CharField(max_length=200, unique=True) # 老师-课程名-学期
    students = models.TextField(blank=True)  # 该班级所有学生的学号
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
    
    def toDict(self):
        return {
            'id': str(self.id),
            'classname': str(self.classname),
            'students': str(self.students),
        }

class Register(models.Model):
    student = models.ForeignKey(Student, on_delete=models.CASCADE)
    face = models.TextField(blank=True)
    create_time = models.DateTimeField(auto_now=False)
    time = models.DateTimeField(auto_now=False) # 签到时间默认为创建记录时间
    clas = models.ForeignKey(Class, on_delete=models.CASCADE)
    result = models.CharField(max_length=100)

    def toDict(self):
        return {
            'id': str(self.id),
            'student_id': str(self.student.id),
            'face': str(self.face),
            'time': str(self.convert_timezone(self.time)),
            'create_time': str(self.convert_timezone(self.create_time)),
            'class_id': str(self.clas.id),
            'result': str(self.result)
        }
    
    def convert_timezone(self, time_in):
        time_utc = time_in.replace(tzinfo=pytz.timezone('UTC'))
        time_local = time_utc.astimezone(pytz.timezone('Asia/Shanghai'))
        return time_local.strftime('%Y-%m-%d %H:%M:%S')


